﻿/********************
 * 
 * Kaixin001 Parking Entities, which can directly JSON Deserialized from web page.
 * Parsed by marstone
 * 
 * ******************/


using System;
using System.Collections.Generic;
using System.Text;
using Studio.Network.Utilities;
using System.Drawing;
using System.Resources;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization;

namespace Studio.Network.Kaixin001.Entities
{
    public class Car : ICloneable
    {

        public static readonly int[] COLOR_VALUES = new int[] { 16777215, 13421772, 0, 16711680, 255, 16776960 };

        public static readonly string[] COLOR_NAMES = new string[] { "白色", "银色", "黑色", "红色", "蓝色", "黄色" };

        public static readonly int CAR_COLOR_WHITE_INDEX = 0;
        public static readonly int CAR_COLOR_SILVER_INDEX = 1;
        public static readonly int CAR_COLOR_BLACK_INDEX = 2;
        public static readonly int CAR_COLOR_RED_INDEX = 3;
        public static readonly int CAR_COLOR_BLUE_INDEX = 4;
        public static readonly int CAR_COLOR_YELLOW_INDEX = 5;

        public static readonly int CAR_COLOR_WHITE = COLOR_VALUES[CAR_COLOR_WHITE_INDEX];
        public static readonly int CAR_COLOR_SILVER = COLOR_VALUES[CAR_COLOR_SILVER_INDEX];
        public static readonly int CAR_COLOR_BLACK = COLOR_VALUES[CAR_COLOR_BLACK_INDEX];
        public static readonly int CAR_COLOR_RED = COLOR_VALUES[CAR_COLOR_RED_INDEX];
        public static readonly int CAR_COLOR_BLUE = COLOR_VALUES[CAR_COLOR_BLUE_INDEX];
        public static readonly int CAR_COLOR_YELLOW = COLOR_VALUES[CAR_COLOR_YELLOW_INDEX];


        public static readonly IList<KeyValuePair<int, string>> COLORS = new List<KeyValuePair<int, string>> {
                new KeyValuePair<int, string>(Car.CAR_COLOR_WHITE, Car.COLOR_NAMES[Car.CAR_COLOR_WHITE_INDEX]),
                new KeyValuePair<int, string>(Car.CAR_COLOR_SILVER, Car.COLOR_NAMES[Car.CAR_COLOR_SILVER_INDEX]),
                new KeyValuePair<int, string>(Car.CAR_COLOR_BLACK, Car.COLOR_NAMES[Car.CAR_COLOR_BLACK_INDEX]),
                new KeyValuePair<int, string>(Car.CAR_COLOR_RED, Car.COLOR_NAMES[Car.CAR_COLOR_RED_INDEX]),
                new KeyValuePair<int, string>(Car.CAR_COLOR_BLUE, Car.COLOR_NAMES[Car.CAR_COLOR_BLUE_INDEX]),
                new KeyValuePair<int, string>(Car.CAR_COLOR_YELLOW, Car.COLOR_NAMES[Car.CAR_COLOR_YELLOW_INDEX]),
        };


        private int _carid;
        private int _price;
        private int _neighbor;
        private int _park_uid;
        private int _parkid;
        private int _ctime;
        private string _car_name;
        public string _car_logo_big;
        private string _car_logo_small;
        public string _car_logo_flash;
        private string _car_trademark;
        public string _car_trademarkname;
        private int _car_police;
        private string _park_real_name;
        private string _park_logo20;
        private int _park_profit;

        /// <summary>
        /// Necessary for JSON Import.
        /// </summary>
        public Car() { }

        public Car(int carid)
        {
            this._carid = carid;
        }

        public Car(int carid, int price, string car_name, string car_logo_small)
        {
            this._carid = carid;
            this._price = price;
            this._car_name = car_name;
            this._car_logo_small = car_logo_small;
        }

        public int color_index
        {
            get { return (this._carid % 0x10000); }
        }

        public Color car_color
        { 
            get
            {
                if(this.car_color_value < 0)
                    return Color.Transparent;
                return Color.FromArgb(car_color_value);
            }
        }

        public int car_color_value
        { 
            get
            {
                if (true == string.IsNullOrEmpty(_car_logo_big))
                    return -1;
                int i1 = _car_logo_big.LastIndexOf('-');
                int i2 = _car_logo_big.LastIndexOf('.');
                if (i1 < 0 || i1 >= i2)
                    return -1;

                string color_value = _car_logo_big.Substring(i1 + 1, i2 - i1);
                int i;
                if (false == int.TryParse(color_value, out i))
                    return -1;
                return i;
            }
        }

        public string park_logo20
        {
            get { return _park_logo20; }
            set { _park_logo20 = value; }
        }

        public string car_trademark
        {
            get { return _car_trademark; }
            set { _car_trademark = value; }
        }


        public string car_logo_small
        {
            get { return _car_logo_small; }
            set { _car_logo_small = value; }
        }

        public Image car_logo_small_image
        {
            get 
            {
                string fullUrl = car_logo_small.StartsWith("http") ? car_logo_small : KXInterface.URL_KX_BASE + car_logo_small;
                Image image = new HttpHelper().GetImage(fullUrl);
                return image;
            }
        }

        public Image car_trademark_image
        {
            get
            {
                string fileName = car_trademark.Substring(car_trademark.LastIndexOf("/") + 1);
                string fullUrl = car_trademark.StartsWith("http") ? car_trademark : KXInterface.URL_KX_BASE + car_trademark;

                // search cache first.
                string cacheFile = string.Format("./{0}/{1}", KXInterface.CACHE_FOLDER, fileName);
                if (true == File.Exists(cacheFile))
                {
                    return new Bitmap(cacheFile);
                }
                else
                {
                    Image image = new HttpHelper().GetImage(fullUrl);

                    try
                    {
                        if (false == Directory.Exists(KXInterface.CACHE_FOLDER))
                        {
                            Directory.CreateDirectory(KXInterface.CACHE_FOLDER);
                        }
                        image.Save(cacheFile);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                    }
                    return image;
                }
            }
        }
        public int ctime
        {
            get { return _ctime; }
            set { _ctime = value; }
        }

        public int car_police
        {
            get { return _car_police; }
            set { _car_police = value; }
        }
        public string park_real_name
        {
            get { return _park_real_name; }
            set { _park_real_name = value; }
        }

        public int park_uid
        {
            get { return _park_uid; }
            set { _park_uid = value; }
        }

        public string car_name
        {
            get { return _car_name; }
            set { _car_name = value; }
        }
        public int parkid
        {
            get { return _parkid; }
            set { _parkid = value; }
        }
 
        public int carid
        {
            get { return _carid; }
            set { _carid = value; }
        }

        public int carid_atom
        {
            get { return carid >= (1 << 16) ? carid >> 16 : carid; }
        }

        public int price
        {
            get { return _price; }
            set { _price = value; }
        }   
        public int neighbor
        {
            get { return _neighbor; }
            set { _neighbor = value; }
        }        
        public int park_profit
        {
            get { return _park_profit; }
            set { _park_profit = value; }
        }

        public object Clone()
        {
            Car car = new Car(this._carid, this._price, this._car_name, this._car_logo_small);
            car.car_police = this._car_police;
            car.car_trademark = this._car_trademark;
            car.ctime = this._ctime;
            car.neighbor = this._neighbor;
            car.park_logo20 = this._park_logo20;
            car.park_uid = this._park_uid;
            car.park_real_name = this._park_real_name;
            car.park_profit = this._park_profit;
            car.park_logo20 = this._park_logo20;
            return car;
        }

        public override bool Equals(object obj)
        {
            if (obj is Car)
            {
                Car car = obj as Car;
                return car.carid_atom == this.carid_atom;
            }
            return false;
        }
    }
}
